﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.Stocks;
using System.Data.SqlClient;
using System.Data;
using ChangeHope.DataBase;

namespace YXShop.SQLServerDAL.Stocks
{
    class ProductTruck : IProductTruck
    {
        #region IProductTruck 成员

        #region DataBase Operation
        public int Add(Model.Stocks.ProductTruck model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_producttruck(");
            strSql.Append("barcodeid,adminid,operationTime,operationType,storeType,storeid)");
            strSql.Append(" values (");
            strSql.Append("@barcodeid,@adminid,@operationTime,@operationType,@storeType,@storeid)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@barcodeid", SqlDbType.Int,4),
					new SqlParameter("@adminid", SqlDbType.Int,4),
					new SqlParameter("@operationTime", SqlDbType.DateTime),
					new SqlParameter("@operationType", SqlDbType.Int,4),
					new SqlParameter("@storeType", SqlDbType.Int,4),
					new SqlParameter("@storeid", SqlDbType.Int,4)};
            parameters[0].Value = model.BarcodeID;
            parameters[1].Value = model.Adminid;
            parameters[2].Value = model.OperationTime;
            parameters[3].Value = (int)model.OperationType;
            parameters[4].Value = (int)model.StoreType;
            parameters[5].Value = model.StoreID;

            object obj = SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Model.Stocks.ProductTruck model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_producttruck set ");
            strSql.Append("barcodeid=@barcodeid,");
            strSql.Append("adminid=@adminid,");
            strSql.Append("operationTime=@operationTime,");
            strSql.Append("operationType=@operationType,");
            strSql.Append("storeType=@storeType,");
            strSql.Append("storeid=@storeid");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@barcodeid", SqlDbType.Int,4),
					new SqlParameter("@adminid", SqlDbType.Int,4),
					new SqlParameter("@operationTime", SqlDbType.DateTime),
					new SqlParameter("@operationType", SqlDbType.Int,4),
					new SqlParameter("@storeType", SqlDbType.Int,4),
					new SqlParameter("@storeid", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.BarcodeID;
            parameters[2].Value = model.Adminid;
            parameters[3].Value = model.OperationTime;
            parameters[4].Value = (int)model.OperationType;
            parameters[5].Value = (int)model.StoreType;
            parameters[6].Value = model.StoreID;

            return SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from yxs_producttruck ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void DeleteAll(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_producttruck] where id in(");
            strSql.Append(" " + ids + " )");
            SQLServerHelper.ExecuteSql(strSql.ToString());
        }
        #endregion

        #region DataBase Load
        public Model.Stocks.ProductTruck GetModelById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,barcodeid,adminid,operationTime,operationType,storeType,storeid from yxs_producttruck ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.Stocks.ProductTruck model = new Model.Stocks.ProductTruck();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["barcodeid"].ToString() != "")
                {
                    model.BarcodeID = int.Parse(ds.Tables[0].Rows[0]["barcodeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["adminid"].ToString() != "")
                {
                    model.Adminid = int.Parse(ds.Tables[0].Rows[0]["adminid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["operationTime"].ToString() != "")
                {
                    model.OperationTime = DateTime.Parse(ds.Tables[0].Rows[0]["operationTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["operationType"].ToString() != "")
                {
                    model.OperationType = (Model.Stocks.ProductStateEnum)int.Parse(ds.Tables[0].Rows[0]["operationType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["storeType"].ToString() != "")
                {
                    model.StoreType = (Model.Stocks.StockTypeEnum)int.Parse(ds.Tables[0].Rows[0]["storeType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["storeid"].ToString() != "")
                {
                    model.StoreID = int.Parse(ds.Tables[0].Rows[0]["storeid"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        public ChangeHope.DataBase.DataByPage GetList(string strWhere)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("[select] * [from] yxs_producttruck [where] 1=1 ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("" + strWhere);
            }
            strSql.Append(" [order by] id desc");
            dataPage.Sql = strSql.ToString();
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        public List<Model.Stocks.ProductTruck> GetListProductTruckByWhere(string where)
        {
            List<Model.Stocks.ProductTruck> modelList = new List<Model.Stocks.ProductTruck>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,barcodeid,adminid,operationTime,operationType,storeType,storeid ");
            strSql.Append(" FROM yxs_producttruck ");
            if (where.Trim() != "")
            {
                strSql.Append(" where " + where);
            }
            DataSet ds= SQLServerHelper.Query(strSql.ToString());
            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
               
                int rowsCount = dt.Rows.Count;
                if (rowsCount > 0)
                {
                    Model.Stocks.ProductTruck model;
                    for (int n = 0; n < rowsCount; n++)
                    {
                        model = new Model.Stocks.ProductTruck();
                        if (dt.Rows[n]["id"].ToString() != "")
                        {
                            model.ID = int.Parse(dt.Rows[n]["id"].ToString());
                        }
                        if (dt.Rows[n]["barcodeid"].ToString() != "")
                        {
                            model.BarcodeID = int.Parse(dt.Rows[n]["barcodeid"].ToString());
                        }
                        if (ds.Tables[0].Rows[0]["adminid"].ToString() != "")
                        {
                            model.Adminid = int.Parse(ds.Tables[0].Rows[0]["adminid"].ToString());
                        }
                        if (dt.Rows[n]["operationTime"].ToString() != "")
                        {
                            model.OperationTime = DateTime.Parse(dt.Rows[n]["operationTime"].ToString());
                        }
                        if (dt.Rows[n]["operationType"].ToString() != "")
                        {
                            model.OperationType = (Model.Stocks.ProductStateEnum)int.Parse(dt.Rows[n]["operationType"].ToString());
                        }
                        if (dt.Rows[n]["storeType"].ToString() != "")
                        {
                            model.StoreType = (Model.Stocks.StockTypeEnum)int.Parse(dt.Rows[n]["storeType"].ToString());
                        }
                        if (dt.Rows[n]["storeid"].ToString() != "")
                        {
                            model.StoreID = int.Parse(dt.Rows[n]["storeid"].ToString());
                        }
                        modelList.Add(model);
                    }
                }
               
            }
            return modelList;
        }

        public ChangeHope.DataBase.DataByPage GetList(string orderfield, int pagesize, string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_producttruck [where] 1=1 " + Conditions + " " + orderfield;
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        #endregion
        #endregion
        #region Other Function
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDataSetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,barcodeid,adminid,operationTime,operationType,storeType,storeid ");
            strSql.Append(" FROM yxs_producttruck ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLServerHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,barcodeid,adminid,operationTime,operationType,storeType,storeid ");
            strSql.Append(" FROM yxs_producttruck ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLServerHelper.Query(strSql.ToString());
        }
        public IDbDataParameter[] ValueParas(YXShop.Model.Stocks.ProductTruck model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@barcodeid", SqlDbType.Int,4),
					new SqlParameter("@adminid", SqlDbType.Int,4),
					new SqlParameter("@operationTime", SqlDbType.DateTime),
					new SqlParameter("@operationType", SqlDbType.Int,4),
					new SqlParameter("@storeType", SqlDbType.Int,4),
					new SqlParameter("@storeid", SqlDbType.Int,4)};
            parameters[0].Value = model.BarcodeID;
            parameters[1].Value = model.Adminid;
            parameters[2].Value = model.OperationTime;
            parameters[3].Value = (int)model.OperationType;
            parameters[4].Value = (int)model.StoreType;
            parameters[5].Value = model.StoreID;
            return parameters;
        }
        #endregion
    }
}
